<template>
	<!-- <page-meta :page-style="'overflow:'+(popupShow?'hidden':'visible')"></page-meta> -->
	<view class="container">
		<HeadNav title="店铺活动详情" back />
		<view class="detailinfo">
<uni-section title="基本信息" type="line" titleFontSize="16px" titleColor="#BC9F5E">
			<view class="detail-item">
				<view class="icon-normal"></view>
				<view class="detail-item-right">
					<text>活动名称</text>
					<text class="detail-item-value">{{detailInfo?.activityName}}</text>
				</view>
			</view>
			<view class="detail-item">
				<view class="icon-normal"></view>
				<view class="detail-item-right">
					<text>活动类型</text>
					<text class="detail-item-value"
						v-if="detailInfo.activityType">{{activityTypeFormat(detailInfo.activityType)}}</text>
				</view>
			</view>
			<view class="detail-item">
				<view class="icon-normal"></view>
				<view class="detail-item-right">
					<text>所在城市</text>
					<text class="detail-item-value">{{detailInfo?.provinceCodeText}}{{detailInfo?.cityCodeText}}</text>
				</view>
			</view>
			<view class="detail-item">
				<view class="icon-fy"></view>
				<view class="detail-item-right">
					<text>活动费用</text>
					<text class="detail-item-value">￥{{detailInfo.price}}</text>
				</view>
			</view>
			<view class="detail-item">
				<view class="icon-time"></view>
				<view class="detail-item-right">
					<text>活动时间</text>
					<text
						class="detail-item-value">{{formatDate(detailInfo.startTime)}}~{{formatDate(detailInfo.endTime)}}</text>
				</view>
			</view>
			<view class="detail-item">
				<view class="icon-time"></view>
				<view class="detail-item-right">
					<text>报名时间</text>
					<text
						class="detail-item-value">{{formatDate(detailInfo.entryStartTime)}}~{{formatDate(detailInfo.entryEndTime)}}</text>
				</view>
			</view>
			<!-- <view class="detail-item">
				<uni-icons type="email" size="16" style="position: relative;top: 2rpx;"></uni-icons>
				<view class="detail-item-right">
					<text>举办时间段</text>
					<text class="detail-item-value">14:00~18:00</text>
				</view>
			</view> -->
			<view class="detail-item">
				<view class="icon-normal"></view>
				<view class="detail-item-right">
					<text>活动地址</text>
					<text class="detail-item-value">{{detailInfo?.address}}</text>
				</view>
			</view>
		</uni-section>
		<uni-section title="活动主办方" type="line" titleFontSize="16px" titleColor="#BC9F5E">
			<navigator :url="'/pages/match/store?pageType=activity&id='+ detailInfo.storeId">
				<view class="storeinfo">
					<view class="storeinfo-left">
						<image v-if="detailInfo.storeUrl" class="product-item-img" :src="detailInfo.storeUrl">
						</image>
						<view v-else class="no-image"></view>
					</view>
					<view class="storeinfo-name">
						{{detailInfo?.storeName}}
					</view>
					<view class="storeinfo-right">
						<uni-icons type="paperplane-filled" size="30"></uni-icons>
					</view>
				</view>
			</navigator>

		</uni-section>
		</view>
		
		<view class="content-footer">
			<!-- <view class="button-bar-btn" :class="{disabled: signUpBtn.disabled}" @tap="showSearchPopup">
				{{signUpBtn.text}}
			</view> -->
			<view class="button-bar-btn" @tap="handleScan">
				<uni-icons type="scan" size="20" color="#fff" style="margin-right: 8rpx;"></uni-icons>
				扫码核销
			</view>
		</view>
	</view>
	<!-- 普通弹窗 -->
	<!-- <uni-popup ref="popupRef" background-color="#FEFEFE" type="bottom" @change="popupChange">
		<view class="popup-content">
			<view class="popupinfo">
				<view class="popupinfo-item">
					<view class="popupinfo-item-key">
						<uni-icons type="email" size="16"></uni-icons>
						<text class="key-text">活动时间</text>
					</view>
					<text
						class="value-text">{{formatDate(detailInfo.startTime)}}~{{formatDate(detailInfo.endTime)}}</text>
				</view>
				<view class="popupinfo-item">
					<view class="popupinfo-item-key">
						<uni-icons type="email" size="16"></uni-icons>
						<text class="key-text">活动费用</text>
					</view>
					<text class="value-text">￥{{detailInfo.price}}</text>
				</view>
				<view class="popupinfo-item">
					<view class="popupinfo-item-key">
						<uni-icons type="email" size="16"></uni-icons>
						<text class="key-text">支付方式</text>
					</view>
					<text class="value-text">到店付款</text>
				</view>
		
			</view>
			<view class="popup-bottom">
				<view class="popup-bottom-item">
					<uni-icons type="email" size="16"></uni-icons>
					<view class="detail-item-right">
						<text>用户手机号</text>
						<text class="detail-item-value">{{userStore.user.phone}}</text>
					</view>
				</view>
				<view class="button-area">
					<view class="bottom-btn-cancel" @tap="closePopup">
						取消
					</view>
					<view class="bottom-btn-sure" @tap="handleSignup">
						确定
					</view>
				</view>
			</view>
		</view>
	</uni-popup> -->
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';
	import {
		activityDetailApi,
		timeEnrollApi,
		verifyActivityApi
	} from '@/api/activity'
	import {
		onLoad,
		onShareAppMessage
	} from '@dcloudio/uni-app'
	import {
		formatDate
	} from '@/utils/formatTime'
	import {
		dictionaryApi
	} from '@/api/common'
	import HeadNav from '@/components/HeadNav'
	// import {
	// 	useUserStore
	// } from '@/stores'
	// const popupRef = ref(null)
	// const popupShow = ref(false)
	const detailInfo = ref({})
	const signUpBtn = reactive({
		disabled: true,
		text: ''
	})
	let activityId = ''
	// const userStore = useUserStore()
	let activeMultiArray = []

	onLoad(options => {
		console.log('options', options)
		activityId = options.id
		getData(options.id)
	})

	const getData = async (id) => {
		try {
			uni.showLoading({
				title: '正在加载...'
			});
			checkActiveSignup(id)
			const res = await activityDetailApi(id);
			const dictRes = await dictionaryApi('activity_type_dict')
			activeMultiArray = dictRes.data
			detailInfo.value = res.data
			uni.hideLoading()
		} catch (error) {
			uni.hideLoading()
			uni.showToast({
				title: '查询活动数据失败，请稍后再试',
				icon: 'error'
			})
		}
	}

	const checkActiveSignup = (id) => {
		timeEnrollApi({
			activityId: id
		}).then(res => {
			if (res.data.isShow) {
				// 正常报名
				signUpBtn.disabled = false
			} else {
				signUpBtn.disabled = true
			}
			signUpBtn.text = res.data.msg
		})

	}

	const handleScan = () => {
		uni.scanCode({
			success: (res) => {
				// console.log(res)
				if (res.scanType === 'QR_CODE' && res.result) {
					console.log('扫描结果：', res.result);
					const test = /^\d+$/.test(res.result);
					if (test) {
						// 调接口校验
						uni.showLoading({
							title: '正在校验...',
							mask: true
						});
						verifyActivityApi({
							id: detailInfo.value.id, //活动id
							userId: res.result //app用户id
						}).then(() => {
							uni.hideLoading()
							uni.showToast({
								title: '核销成功',
								duration: 2000
							});
						}).catch(() => uni.hideLoading())
					} else {
						uni.showToast({
							title: '二维码扫描异常，请重新尝试',
							icon: 'none'
						});
					}
				} else {
					uni.showToast({
						title: '二维码扫描异常，请重新尝试',
						icon: 'none'
					});
				}
			},
			fail: (err) => {
				if (err.errMsg.includes('cancel')) {
					console.log('扫描已取消')
				} else {
					console.log('err', err)
					uni.showToast({
						title: '当前二维码无法识别，请检查',
						icon: 'none'
					});
				}
			}
		});
	}

	const activityTypeFormat = (value) => {
		const target = activeMultiArray.find(item => item.dictValue === value)
		// console.log('target', target)
		return target ? target.dictLabel : ''
	}



	// const handleSignup = async () => {
	// 	uni.showLoading({
	// 		title: '正在报名...'
	// 	});
	// 	await signUpApi({
	// 		activityId: activityId
	// 	})
	// 	uni.hideLoading()
	// 	uni.showToast({
	// 		title: '报名成功'
	// 	});
	// 	popupRef.value.close()
	// }

	// const showSearchPopup = () => {
	// 	if (signUpBtn.disabled) {
	// 		return
	// 	} else {
	// 		popupRef.value.open()
	// 	}

	// }

	// const closePopup = () => {
	// 	popupRef.value.close()
	// }

	// const popupChange = (e) => {
	// 	popupShow.value = e.show
	// }

	// const gotoMyActive = () => {
	// 	uni.navigateTo({
	// 		url: '/pages/my/my-active'
	// 	})
	// }
</script>

<style lang="scss" scoped>
	.container {
		background-color: #EEEEEE;
		// padding: 32rpx;
		box-sizing: border-box;
		font-family: PingFangSC-Regular, sans-serif;
		min-height: 100vh;
		padding-bottom: env(safe-area-inset-bottom);
	}
	.detailinfo {
padding: 32rpx;
	}

	.detail-item {
		background-color: #E8E8E8;
		padding: 16rpx;
		display: flex;
		align-items: center;
		gap: 24rpx;
		font-size: 28rpx;
		margin-bottom: 20rpx;
		.icon-normal {
			width: 28rpx;
			height: 28rpx;
			background: url('https://ucp-tcg.com:18001/mc/app/static/icon_xx.png') no-repeat;
			background-size: contain;
		}

		.icon-fy {
			width: 28rpx;
			height: 28rpx;
			background: url('https://ucp-tcg.com:18001/mc/app/static/icon_fy.png') no-repeat;
			background-size: contain;
		}

		.icon-time {
			width: 28rpx;
			height: 28rpx;
			background: url('https://ucp-tcg.com:18001/mc/app/static/icon_sj.png') no-repeat;
			background-size: contain;
		}

		.detail-item-right {
			display: flex;
			justify-content: space-between;
			align-items: center;
			flex: 1;
		}

		.detail-item-value {
			color: #ABABAB;
			max-width: 70%;
		}
	}

	.storeinfo {
		padding: 10rpx;
		border: 2rpx solid #D2CAB8;
		background-color: #fff;
		display: flex;
		gap: 40rpx;

		.storeinfo-left {
			width: 150rpx;
			height: 150rpx;

			image {
				width: 100%;
				height: 100%;
			}

			.no-image {
				width: 100%;
				height: 100%;
				background-color: $no-image-color;
			}
		}

		.storeinfo-name {
			font-size: 32rpx;
			font-weight: 600;
			display: inline-flex;
			align-items: center;
		}

		.storeinfo-right {
			display: flex;
			flex: 1;
			justify-content: flex-end;
			padding-right: 20rpx;
			align-items: center;
		}
	}

	.content-footer {
		margin-top: 40rpx;
		border-top: 2rpx solid #DEDEDE;
		padding-top: 65rpx;
		display: flex;
		align-items: center;
		flex-direction: column;

		.button-bar-btn {
			font-family: PingFangSC-Regular, sans-serif;
			background-color: #333;
			color: #fff;
			font-size: 32rpx;
			padding: 16rpx 0;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 55%;
			border-radius: 12rpx;
			margin-bottom: 40rpx;

			&.disabled {
				color: rgba(0, 0, 0, .3);
				background-color: #f7f7f7;
				border: 2rpx solid rgba(0, 0, 0, .2);
			}
		}
	}


	// .popup-content {
	// 	// @include flex;
	// 	// align-items: center;
	// 	// justify-content: center;
	// 	padding: 15px 0;
	// 	height: 40vh;
	// }

	// .popupinfo-item {
	// 	display: flex;
	// 	justify-content: space-between;
	// 	font-size: 28rpx;
	// 	margin-bottom: 30rpx;
	// 	padding: 0 32rpx;

	// 	.key-text {
	// 		margin-left: 16rpx;
	// 	}

	// 	.value-text {
	// 		color: #ABABAB;
	// 	}
	// }

	// .popup-bottom {
	// 	padding: 40rpx 16rpx;
	// 	border-top: 2rpx solid #DEDEDE;
	// }

	// .popup-bottom-item {
	// 	background-color: #E8E8E8;
	// 	padding: 16rpx;
	// 	display: flex;
	// 	align-items: center;
	// 	gap: 24rpx;
	// 	font-size: 28rpx;

	// 	.detail-item-right {
	// 		display: flex;
	// 		justify-content: space-between;
	// 		align-items: center;
	// 		flex: 1;
	// 	}


	// 	.detail-item-value {
	// 		color: #ABABAB;
	// 	}
	// }

	// .button-area {
	// 	display: flex;
	// 	gap: 100rpx;
	// 	justify-content: center;
	// 	width: 100%;
	// 	padding: 0 32rpx;
	// 	padding-top: 60rpx;
	// 	box-sizing: border-box;

	// 	.bottom-btn-cancel {
	// 		border-radius: 12rpx;
	// 		border: 2rpx solid #CCCCCC;
	// 		background-color: #E1E1E1;
	// 		// width: 264rpx;
	// 		height: 72rpx;
	// 		// line-height: 72rpx;
	// 		width: 40%;
	// 		color: #333;
	// 		display: flex;
	// 		align-items: center;
	// 		justify-content: center;
	// 		font-size: 28rpx;
	// 		// font-weight: 600;
	// 	}

	// 	.bottom-btn-sure {
	// 		border-radius: 12rpx;
	// 		border: 2rpx solid #CCCCCC;
	// 		background-color: #333333;
	// 		color: #fff;
	// 		height: 72rpx;
	// 		width: 40%;
	// 		// padding: 16rpx 0;
	// 		display: flex;
	// 		align-items: center;
	// 		justify-content: center;
	// 		color: #fff;
	// 		font-size: 28rpx;
	// 		// font-weight: 600;
	// 	}
	// }

	::v-deep .uni-section {
		background-color: #EEEEEE;
	}

	::v-deep .uni-section .uni-section-header__content {
		font-weight: bold;
	}

	::v-deep .uni-section .uni-section-header__decoration {
		background-color: #BB9F5E;
	}

	::v-deep .uni-section .uni-section-content {
		// padding-left: 36rpx !important;
		// padding-right: 36rpx !important;
		padding-bottom: 24rpx;
	}
</style>